﻿using System;
using System.Configuration;
using System.IO;
using System.Linq;
using Controllers;
using CuteWebUI;
using File = Controllers.File;

namespace Web
{
    public partial class NoticeList : System.Web.UI.Page
    {
        protected string trDisplay;
        protected string isAuthed = " noAuthed";
        protected void Page_Load(object sender, EventArgs e)
        {
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            //超级管理员角色
            if (ebi.EmpRoles.Where(r => r.RoleId==1).Any())
            {
                isAuthed = "";
            }
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            string pId = Request.QueryString["pId"];
            string isUp = Request.QueryString["isUp"];
            int? Pid = null;
            if (!string.IsNullOrEmpty(pId))
            {
                Pid = int.Parse(pId);
            }
            if (!string.IsNullOrEmpty(isUp))
            {
                File f = ctx.Files.Single(r => r.Id == Pid);
                Pid = f.Pid;
            }
            if (Pid == null)
            {
                trDisplay = " style='display:none;'";
            }
            var list = from r in ctx.Files where r.FileClass ==2 && (r.Pid == Pid || Nullable.Equals(r.Pid, Pid)) join r2 in ctx.Employees on r.OwnerId equals r2.Id orderby r.OrderNo, r.CreatTime descending select new { r.Id, r.Name, r.Size, EmpName = r2.Name, r.CreatTime, r.Type, r.FileClass, r.RealName };
            rptList.DataSource = list;
            rptList.DataBind();
        }

        protected string getSize(object size)
        {
            string retStr = null;
            if (size != null && size != "0")
            {
                retStr = (int.Parse(size.ToString()) / 1024).ToString();
            }
            return retStr;
        }

        protected string getIcon(string type)
        {
            string retStr = type;
            string validExt = "com,doc,exe,gif,htm,jpg,ppt,rar,txt,xls,zip,dir";
            if (validExt.IndexOf(type) < 0)
            {
                retStr = "com";
            }
            return retStr;
        }

        protected string getLinks(string idStr, string type, string fileClass, object realName)
        {
            string retStr = "";
            if (type != "dir")
            {
                if ("xls,xlsx,doc,docx".IndexOf(type) >= 0)
                {
                    retStr += "<span onclick='view(" + fileClass + ",\"" + realName + "\"," + idStr + ");'>[查看]</span>";
                }
                retStr += "<span onclick='singleDownload(" + idStr + ");'>[下载]</span>";
                retStr += "<span onclick='ReadLog(" + idStr + ");'>[日志]</span>";
            }
            return retStr;
        }

        protected void Uploader_File(object sender, UploaderEventArgs args)
        {
            string m_path = Server.MapPath("/NoticeFolder/");
            int dotIndex = args.FileName.LastIndexOf('.');
            string fileExt = args.FileName.Substring(dotIndex);//含.
            string filename = args.FileName.Substring(0, dotIndex);//原文件名
            string newFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + fileExt;//保存文件名
            string savePath = m_path + newFileName;//保存路径
            args.MoveTo(savePath);

            try
            {
                ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                File file = new File();
                file.FileClass =2;
                EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                file.OwnerId = ebi.Id;
                if (Request.QueryString["pId"] != null)
                {
                    file.Pid = int.Parse(Request.QueryString["pId"]);
                }
                else
                {
                    file.Pid = null;
                }
                file.RealName = newFileName;
                file.Type = fileExt.Substring(1);
                file.CreatTime = DateTime.Now;
                ctx.Files.InsertOnSubmit(file);
                file.Name = filename;
                FileInfo f = new FileInfo(savePath);
                file.Size = (int)f.Length;
                //写数据库
                ctx.SubmitChanges();
                Response.Redirect(this.Request.Url.ToString());
            }
            catch (Exception ex)
            {
            }
        }
    }
}